package cn.fancy.sdk.common.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkImpl 
{
	
	/**
	 * 获取网络类型
	 * @param ctx
	 * @return
	 */
	public static String getNetworkTypeName(Context ctx) 
	{
		ConnectivityManager mgr = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetworkInfo = mgr.getActiveNetworkInfo();
		
		if (activeNetworkInfo == null) {
			return null;
		}
		
		String extraInfo = activeNetworkInfo.getExtraInfo();
		
		if (extraInfo != null && extraInfo.length() > 0)
		{
			return extraInfo.replace("\"", "");
		}
		return activeNetworkInfo.getTypeName().replace("\"", "");
	}

	
	/**
	 * 检测网络是否可用
	 * 
	 * @return
	 */
	
	public static boolean isNetworkConnected(Context context) 
	{
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		
		NetworkInfo ni = cm.getActiveNetworkInfo();
		
		return ni != null && ni.isConnectedOrConnecting();
	}
	
	/**
	 * 判断网络是2g/3g,或者是wifi环境
	 * @param context
	 * @return
	 */
	public static boolean isWifiNetWork(Context context){
		ConnectivityManager connectMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = connectMgr.getActiveNetworkInfo();
		if(info != null)
		{
			if(info.getType() == ConnectivityManager.TYPE_WIFI){
				return true;
			}
			
			if( info.getType() ==  ConnectivityManager.TYPE_MOBILE){
				return false;
			}
		}
		return false;
	}
}
